Repository navigation
Add union type - #766
Add union type#766bvisness wants to merge 2 commits into
Conversation
Just a bunch of drive-by updates made as part of union/overload work.
|
I have updated this PR to only contain unions, and have moved overloads to #774. |
lukewagner
left a comment
There was a problem hiding this comment.
Overall, looking good, nice work! I haven't looked at JS-Reference.md yet, but here's a few first comments and questions:
| (result <valtype>? (error <valtype>)?) ↦ (variant (case "ok" <valtype>?) (case "error" <valtype>?)) | ||
| string ↦ (list char) | ||
| (map <keytype> <valtype>) ↦ (list (tuple <keytype> <valtype>)) | ||
| (union (case <labellit> <valtype>)+) ↦ (variant (case <labellit> <valtype>)+) |
There was a problem hiding this comment.
ultra-nit: can you put union after result (to put the variants together)
| | '[method]' <label> '.' <label> | ||
| | '[method]' ('[get]' | '[set]') <label> '.' <label> 📡 | ||
| | '[static]' <label> '.' <label> | ||
| | '[static]' ('[get]' | '[set]') <label> '.' <label> 📡 |
There was a problem hiding this comment.
This and the following refactoring and readability enhancements to the name descriptions and strong uniqueness are great, but could you put them into a separate PR (that we can promptly review and merge ahead of this PR).
| languages to pick a case by inspecting the type of a source-language value. | ||
| Statically-typed languages can choose to represent `union`s like `variant`s | ||
| (with explicit case names) or as simple unions of types if their type system | ||
| permits (e.g. `string | number`). |
There was a problem hiding this comment.
| permits (e.g. `string | number`). | |
| permits (e.g. `string | number` in TypeScript). |
| | `(option T)` | [...kinds of T, object] | | ||
| | `(own R)`, `(borrow R)` | [resource `R`] (types that are `eq` are indistinguishable) | | ||
| | `union` | kinds of all case payloads in declaration order, excluding duplicates | | ||
| | `stream`, `future`, `error-context` | *not allowed* | |
There was a problem hiding this comment.
Even if it's not implemented in the Web Preview, it seems like these 3 cases could be defined (emoji-gated), even if they're just object (b/c duck-typing).
| </details> | ||
|
|
||
| Some types are not allowed to appear as a union case even if they have | ||
| distinguishability rules defined: |
There was a problem hiding this comment.
If they are rejected by validation, why do they have distinguishability rules defined? Why not just put them in a single "rejected" row?
There was a problem hiding this comment.
Oh! That's a holdover from overloads, which do perform distinguishability tests on those types. I should shuffle those over to the overloads PR.
Adds support for
union(a variant ofvariant). The design of unions is mostly unchanged from what @eqrion originally proposed (maybe modulo some of the mapping from types to distinguishable kinds).Resolves #724.
One thing I wanted to discuss is the distinguishability rules (which may eventually be reused for overloads). It's really hard to find a set of "distinguishable kinds" for each type that will actually avoid conflicts in various languages without being totally useless. The danger, in my opinion, is "kind creep", where you keep adding more distinguishable kinds to each component type, e.g. adding the
listkind to the componentmaptype because maps are frequently iterable in source languages. This reduces ambiguity for bindings, but makes unions much less useful.In my opinion the right way to handle this is to be stricter about types when in a "distinguishable context". You can see this reflected in the updated JS API. A plain old
list<string>parameter will accept any JS iterable (in TypeScript terms,Iterable<string>), whereas aunion{ list<string>, ... }parameter requires a JS array (Array<string>). In other words, the runtime would literally runArray.isArrayto decide if the value is "list-like", rather than checking forSymbol.iterator. This should only be a modest complexity cost for devs and one that TypeScript will catch right away. And importantly it ensures that union parameters should round-trip nicely; e.g. aunion{ list, map }will flow out to JS as anArrayorMapand back to components as alistormap, without any awkward crossover.